package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.*;
import com.sc.service.StudyFileService;
import com.sc.service.StudyNoteService;


import com.sc.util.UploadDownloadUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studys")
public class LearnController {
    @Autowired
    StudyNoteService sn;
    @Autowired
    StudyFileService sf;

    @RequestMapping("/stushow")
    public String stushow(@RequestParam(defaultValue = "1") Integer pageNum, Map m) {
        PageInfo<StudyNote> p = sn.stushow(pageNum, 4);
        m.put("p", p);
        return "/study/note";
    }

    @RequestMapping("/update")
    public String update(StudyFile file, HttpServletRequest req,MultipartFile f,  HttpServletResponse resp,HttpSession session) throws IOException {
        String s = UploadDownloadUtil.upload(req,f);
        if (s.equals("")) {
            resp.sendRedirect("/study/list");
        } else {
            file.setFname(s);
            file.setUpldate(new Date());
            file.setPath("/upload" + s);
            User u = (User) session.getAttribute("u");
            file.setId(u.getId());
            sf.addFile(file);
            resp.sendRedirect("/study/list");
        }
        return s;
    }
    //下载功能
    @RequestMapping("/downloadFile")
    public ResponseEntity<byte[]> dowmFile(String fileName, HttpServletRequest request){
        // 读取文件保存的根位置
        String realPath = request.getSession().getServletContext().getRealPath("/upload");
        File file = new File(realPath + "/" + fileName);
        HttpHeaders header = new HttpHeaders();
        // 设置Content-disposition=attachment（下载头）
        header.setContentDispositionFormData("attachment", fileName);
        ResponseEntity<byte[]> re = null;
        try {
            // 将文件转化为流
            re = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), header, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return re;
    }

    //查看
        @RequestMapping("/toshow")
        public String toShow(Integer nid,Map m){
            StudyNote st=sn.selectByPrimaryKey(nid);
            m.put("st",st);
            return "/study/title";
    }
        @RequestMapping("/toAdd")
        public String toAdd () {
            return "/study/add";
        }

        @RequestMapping("/toList")
        public String toList () {
            return "/study/list";
        }

        @RequestMapping("/toNote")
        public String toNote () {
            return "/study/note";
        }
    }
